package com.example.homework9;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

public class Fragment2_3 extends Fragment {
    public Fragment2_3(){
        super(R.layout.fragment2_3);
    }

    String FILE_NAME="123";
    private EditText editTextNumber,editTextPassword;
    private Button button;

    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);

        editTextNumber = (EditText) getView().findViewById(R.id.loginnumber);
        editTextPassword = (EditText) getView().findViewById(R.id.password);

        Sha256 sha256 = new Sha256();

        SharedPreferences settings = getActivity().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);

        String number = settings.getString("name","");
        String num = settings.getString("num","");
        editTextNumber.setText(number);
        editTextPassword.setText(num);

        String Number = editTextNumber.getText().toString();
        String Password = editTextPassword.getText().toString();

        button=getView().findViewById(R.id.loginbutton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Number!=null && Password!=null && !"".equals(number) && !"".equals(num)){
                    Toast.makeText(getActivity(),"登陆成功！",Toast.LENGTH_SHORT).show();
                }else{
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("name",editTextNumber.getText().toString());
                    editor.putString("num",sha256.getSHA256StrJava(editTextPassword.getText().toString()));
                    editor.commit();
                    Toast.makeText(getActivity(),"保存成功",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}